package Servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");

        User user=Util.checkLogin(req);
        if(user==null){
            resp.setStatus(403);
            return;
        }

        String blogId=req.getParameter("blogId");
        if(blogId==null){
            String jsonString=objectMapper.writeValueAsString(user);
            resp.getWriter().write(jsonString);
        }else{
            BlogDao blogDao=new BlogDao();
            Blog blog=blogDao.selectOne(Integer.parseInt(blogId));
            UserDao userDao=new UserDao();
            User author=userDao.selectById(blog.getUserId());

            author.setIsYourBlog(author.getUserId()==user.getUserId()?1:0);

            String jsonString=objectMapper.writeValueAsString(author);
            resp.getWriter().write(jsonString);
        }
    }
}
